package com.young.mylib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;

/**
 * Created by young on 2016/7/19.
 */
public class PreferenceUtil {
    private final static String TAG = "PreferenceUtil";
    private static Context context;
    private static PreferenceUtil instance;
    /**
     * 保存文件 的名称
     */
    public static final String REMEMBERPWD = "rememberPwd";
    public static final String USERNAME = "saveUserName";
    public static final String PWD = "savePwd";

    private String name;

    /**
     * @param context Context is
     */
    public PreferenceUtil(Context context) {
        this.context = context;
        Log.i(TAG, context.getPackageName());
        this.name = context.getPackageName();
        instance=this;
    }

    public static PreferenceUtil getInstance() {
        if (instance == null) {
            instance = new PreferenceUtil(context);
        }
        return instance;
    }
    public long getLongByKey(String key, long defaultValue) {
        if (context == null) {
            Log.e(TAG, "context is null");
            return defaultValue;
        }
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return preference.getLong(key, defaultValue);
    }
    /**
     * 根据key值获取SharedPreferences里保存的String值
     *
     * @param defaultValue
     * @param key
     * @return value
     */
    public String getValueByKey(String key, String defaultValue) {
        if (context == null) {
            Log.e(TAG, "context is null");
            return defaultValue;
        }
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return preference.getString(key, defaultValue);
    }

    /**
     * 根据key值获取SharedPreferences里保存的int值
     *
     * @param defaultValue
     * @param key
     * @return value
     */
    public int getIntByKey(String key, int defaultValue) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return preference.getInt(key, defaultValue);
    }

    public void putLong(String key, long value) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putLong(key, value);
        editor.commit();
    }
    /**
     * 根据key和value往SharedPreferences里存取int值
     *
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 根据key和value往SharedPreferences里存取String值
     *
     * @param key
     * @param value
     */
    public void putString(String key, String value) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 根据key值获取SharedPreferences里保存的boolean值
     *
     * @param
     * @param key
     * @return value
     */
    public boolean getBooleanValueByKey(String key, boolean defaultValue) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return preference.getBoolean(key, defaultValue);
    }

    /**
     * 根据key和value往SharedPreferences里存取boolean值
     *
     * @param key
     * @param value
     */
    public void putBoolean(String key, boolean value) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 根据key值获取SharedPreferences里保存的boolean值
     *
     * @param
     * @param key
     * @return value
     */
    public float getFloatValueByKey(String key, float defaultValue) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return preference.getFloat(key, defaultValue);
    }

    /**
     * 根据key和value往SharedPreferences里存取boolean值
     *
     * @param key
     * @param value
     */
    public void putFloat(String key, float value) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public void putArray(String key, JSONArray array) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preference.edit();
        editor.putString(key, FastJsonUtils.toJSONString(array));
        editor.commit();
    }

    public JSONArray getArray(String key) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return FastJsonUtils.toJSONArray(preference.getString(key, ""));
    }

    /**
     * <p>
     * Description: 是否包含key
     * </p>
     *
     * @param key
     * @return
     * @author: Administrator
     * @update: [updatedate] [changer][change description]
     */
    public boolean contains(String key) {
        SharedPreferences preference = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return preference.contains(key);
    }


}
